﻿/**  版本信息模板在安装目录下，可自行修改。
* address.cs
*
* 功 能： N/A
* 类 名： address
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2018/1/2 22:18:52   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using ZGEDrySaltery.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace ZGEDrySaltery.MySQLDAL
{
	/// <summary>
	/// 数据访问类:address
	/// </summary>
	public partial class address:Iaddress
	{
		public address()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperMySQL.GetMaxID("ADDRESS_ID", "address"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ADDRESS_ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from address");
			strSql.Append(" where ADDRESS_ID=@ADDRESS_ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ADDRESS_ID", MySqlDbType.Int32)
			};
			parameters[0].Value = ADDRESS_ID;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(ZGEDrySaltery.Model.address model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into address(");
			strSql.Append("ADDRESS,CREATE_USER_ID,CREATE_USER_NAME,CREATE_TIME,SORT_INDEX,STATUS,COSTOMER,TEL,REMARK,SEX)");
			strSql.Append(" values (");
			strSql.Append("@ADDRESS,@CREATE_USER_ID,@CREATE_USER_NAME,@CREATE_TIME,@SORT_INDEX,@STATUS,@COSTOMER,@TEL,@REMARK,@SEX)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ADDRESS", MySqlDbType.VarChar,256),
					new MySqlParameter("@CREATE_USER_ID", MySqlDbType.VarChar,16),
					new MySqlParameter("@CREATE_USER_NAME", MySqlDbType.VarChar,32),
					new MySqlParameter("@CREATE_TIME", MySqlDbType.DateTime),
					new MySqlParameter("@SORT_INDEX", MySqlDbType.Int32,11),
					new MySqlParameter("@STATUS", MySqlDbType.VarChar,2),
					new MySqlParameter("@COSTOMER", MySqlDbType.VarChar,32),
					new MySqlParameter("@TEL", MySqlDbType.VarChar,16),
					new MySqlParameter("@REMARK", MySqlDbType.VarChar,256),
					new MySqlParameter("@SEX", MySqlDbType.VarChar,16)};
			parameters[0].Value = model.ADDRESS;
			parameters[1].Value = model.CREATE_USER_ID;
			parameters[2].Value = model.CREATE_USER_NAME;
			parameters[3].Value = model.CREATE_TIME;
			parameters[4].Value = model.SORT_INDEX;
			parameters[5].Value = model.STATUS;
			parameters[6].Value = model.COSTOMER;
			parameters[7].Value = model.TEL;
			parameters[8].Value = model.REMARK;
			parameters[9].Value = model.SEX;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ZGEDrySaltery.Model.address model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update address set ");
			strSql.Append("ADDRESS=@ADDRESS,");
			strSql.Append("CREATE_USER_ID=@CREATE_USER_ID,");
			strSql.Append("CREATE_USER_NAME=@CREATE_USER_NAME,");
			strSql.Append("CREATE_TIME=@CREATE_TIME,");
			strSql.Append("SORT_INDEX=@SORT_INDEX,");
			strSql.Append("STATUS=@STATUS,");
			strSql.Append("COSTOMER=@COSTOMER,");
			strSql.Append("TEL=@TEL,");
			strSql.Append("REMARK=@REMARK,");
			strSql.Append("SEX=@SEX");
			strSql.Append(" where ADDRESS_ID=@ADDRESS_ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ADDRESS", MySqlDbType.VarChar,256),
					new MySqlParameter("@CREATE_USER_ID", MySqlDbType.VarChar,16),
					new MySqlParameter("@CREATE_USER_NAME", MySqlDbType.VarChar,32),
					new MySqlParameter("@CREATE_TIME", MySqlDbType.DateTime),
					new MySqlParameter("@SORT_INDEX", MySqlDbType.Int32,11),
					new MySqlParameter("@STATUS", MySqlDbType.VarChar,2),
					new MySqlParameter("@COSTOMER", MySqlDbType.VarChar,32),
					new MySqlParameter("@TEL", MySqlDbType.VarChar,16),
					new MySqlParameter("@REMARK", MySqlDbType.VarChar,256),
					new MySqlParameter("@SEX", MySqlDbType.VarChar,16),
					new MySqlParameter("@ADDRESS_ID", MySqlDbType.Int32,11)};
			parameters[0].Value = model.ADDRESS;
			parameters[1].Value = model.CREATE_USER_ID;
			parameters[2].Value = model.CREATE_USER_NAME;
			parameters[3].Value = model.CREATE_TIME;
			parameters[4].Value = model.SORT_INDEX;
			parameters[5].Value = model.STATUS;
			parameters[6].Value = model.COSTOMER;
			parameters[7].Value = model.TEL;
			parameters[8].Value = model.REMARK;
			parameters[9].Value = model.SEX;
			parameters[10].Value = model.ADDRESS_ID;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ADDRESS_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from address ");
			strSql.Append(" where ADDRESS_ID=@ADDRESS_ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ADDRESS_ID", MySqlDbType.Int32)
			};
			parameters[0].Value = ADDRESS_ID;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string ADDRESS_IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from address ");
			strSql.Append(" where ADDRESS_ID in ("+ADDRESS_IDlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ZGEDrySaltery.Model.address GetModel(int ADDRESS_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ADDRESS_ID,ADDRESS,CREATE_USER_ID,CREATE_USER_NAME,CREATE_TIME,SORT_INDEX,STATUS,COSTOMER,TEL,REMARK,SEX from address ");
			strSql.Append(" where ADDRESS_ID=@ADDRESS_ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ADDRESS_ID", MySqlDbType.Int32)
			};
			parameters[0].Value = ADDRESS_ID;

			ZGEDrySaltery.Model.address model=new ZGEDrySaltery.Model.address();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ZGEDrySaltery.Model.address DataRowToModel(DataRow row)
		{
			ZGEDrySaltery.Model.address model=new ZGEDrySaltery.Model.address();
			if (row != null)
			{
				if(row["ADDRESS_ID"]!=null && row["ADDRESS_ID"].ToString()!="")
				{
					model.ADDRESS_ID=int.Parse(row["ADDRESS_ID"].ToString());
				}
				if(row["ADDRESS"]!=null)
				{
					model.ADDRESS=row["ADDRESS"].ToString();
				}
				if(row["CREATE_USER_ID"]!=null)
				{
					model.CREATE_USER_ID=row["CREATE_USER_ID"].ToString();
				}
				if(row["CREATE_USER_NAME"]!=null)
				{
					model.CREATE_USER_NAME=row["CREATE_USER_NAME"].ToString();
				}
				if(row["CREATE_TIME"]!=null && row["CREATE_TIME"].ToString()!="")
				{
					model.CREATE_TIME=DateTime.Parse(row["CREATE_TIME"].ToString());
				}
				if(row["SORT_INDEX"]!=null && row["SORT_INDEX"].ToString()!="")
				{
					model.SORT_INDEX=int.Parse(row["SORT_INDEX"].ToString());
				}
				if(row["STATUS"]!=null)
				{
					model.STATUS=row["STATUS"].ToString();
				}
				if(row["COSTOMER"]!=null)
				{
					model.COSTOMER=row["COSTOMER"].ToString();
				}
				if(row["TEL"]!=null)
				{
					model.TEL=row["TEL"].ToString();
				}
				if(row["REMARK"]!=null)
				{
					model.REMARK=row["REMARK"].ToString();
				}
				if(row["SEX"]!=null)
				{
					model.SEX=row["SEX"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ADDRESS_ID,ADDRESS,CREATE_USER_ID,CREATE_USER_NAME,CREATE_TIME,SORT_INDEX,STATUS,COSTOMER,TEL,REMARK,SEX ");
			strSql.Append(" FROM address ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM address ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ADDRESS_ID desc");
			}
			strSql.Append(")AS Row, T.*  from address T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "address";
			parameters[1].Value = "ADDRESS_ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

